#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTimerEvent>

#include "dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent), m_counter1(0), m_counter2(0)
{
    m_label1 = new QLabel("TIMER1: 0", this);
    m_label2 = new QLabel("TIMER2: 0", this);
    m_setButton = new QPushButton("SET", this);
    m_stopButton = new QPushButton("STOP", this);
    m_valueLine = new QLineEdit("1000", this);
    m_comboBox = new QComboBox(this);
    m_comboBox->addItem("TIMER1", m_idTimer1);
    m_comboBox->addItem("TIMER2", m_idTimer2);

    setLayout(new QVBoxLayout);
    layout()->addWidget (m_label1);
    layout()->addWidget (m_label2);
    layout()->addWidget (m_comboBox);

    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->addWidget (m_valueLine);
    hlayout->addWidget (m_setButton);
    layout()->addItem (hlayout);

    layout()->addWidget (m_stopButton);

    connect(m_setButton, SIGNAL(clicked()), this, SLOT(setTimer()));
    connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stopTimer()));
}

void Dialog::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == m_idTimer1)
        m_label1->setText("TIMER1: " + QString::number(++m_counter1));
    else if(event->timerId() == m_idTimer2)
        m_label2->setText("TIMER2: " + QString::number(++m_counter2));
}

void Dialog::setTimer()
{
    if (m_comboBox->currentIndex() == 0)
    {
        killTimer(m_idTimer1);
        m_idTimer1 = startTimer(m_valueLine->text().toInt());
    }
    else if (m_comboBox->currentIndex() == 1)
    {
        killTimer(m_idTimer2);
        m_idTimer2 = startTimer(m_valueLine->text().toInt());
    }
}

void Dialog::stopTimer()
{
    if(m_comboBox->currentIndex() == 0)
        killTimer(m_idTimer1);
    else if(m_comboBox->currentIndex() == 1)
        killTimer(m_idTimer2);
}
